//
//  RBGoodsOrderDetailVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/15.
//

#import "RBGoodsOrderDetailVC.h"
#import "RBGoodsOrderResponse.h" //model

#import "RBGoodsOrderAppraiseVC.h"
#import "RBGoodsOrderApplyServicePopVC.h"
#import "RBGoodsOrderReundSubmitVC.h"
#import "RBGoodsOrderServiceSubmitVC.h"
#import "RBCashierVC.h" //收银台
#import "RBPublishEvaluateVC.h" //评价
#import "RBLogisticsVC.h" //物流

@interface RBGoodsOrderDetailVC ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) RBGoodsOrderDetailResponse *response;

@end

@implementation RBGoodsOrderDetailVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private
- (void)setup {
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData {
    [self.scrollView view_clear];
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    //model
    RBOrderDetailModel *model = self.response.data;
    //商品信息
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        //店铺
        [view view_addView:[self shopViewWithData:model] withHeight:Zoom(43) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        //商品信息
        [view view_addView:[self goodsViewWithData:model] withHeight:Zoom(97) width:CGFLOAT_MIN top:Zoom(12) bottom:Zoom(12) left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(14) right:Zoom(14) centerX:0];
    //订单信息
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[self itemWithTitle:@"订单编号" des:model.order_number desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"下单时间" des:model.create_time desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        NSString *payTypeString = @""; //支付方式 0未支付 1微信 2支付宝 3余额 4好友代付
        switch ([model.pay_type intValue]) {
            case 0 : {
                payTypeString = @"未支付";
            }   break;
            case 1 : {
                payTypeString = @"微信支付";
            }   break;
            case 2 : {
                payTypeString = @"支付宝支付";
            }   break;
            case 3 : {
                payTypeString = @"余额支付";
            }   break;
            case 4 : {
                payTypeString = @"好友代付";
            }   break;
        }
        [view view_addView:[self itemWithTitle:@"支付方式" des:payTypeString desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"支付时间" des:model.pay_time desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(14) right:Zoom(14) centerX:0];
    //快递
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        NSString *mailType = @""; //邮寄方式【1-快递 | 2-自提】
        NSString *titleString = @"";
        if ([self.response.data.mail_type isEqualToString:@"1"]) {
            mailType = @"快递";
            titleString = @"收货地址";
        } else if ([self.response.data.mail_type isEqualToString:@"2"]) {
            mailType = @"自提";
            titleString = @"自提点";
        }
        [view view_addView:[self itemWithTitle:@"配送方式" des:mailType desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:titleString des:@"" desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:({
            NSString *address = @"";
            if ([self.response.data.mail_type isEqualToString:@"1"]) {
                address = [NSString stringWithFormat:@"%@  %@", model.consignee_name, model.consignee_phone];
            } else if ([self.response.data.mail_type isEqualToString:@"2"]) {
                address = [NSString stringWithFormat:@"%@", model.ziti_info.address];
            }
            
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:@"#555555" align:0 text:address];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:Zoom(16) right:Zoom(16) centerX:0];
        [view view_addView:({
            NSString *address = @"";
            if ([self.response.data.mail_type isEqualToString:@"1"]) {
                address = model.full_address;
            } else if ([self.response.data.mail_type isEqualToString:@"2"]) {
                address = model.ziti_info.title;
            }
            
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:@"#555555" align:0 text:address];
            view.numberOfLines = 0;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:Zoom(16) right:Zoom(16) centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(14) right:Zoom(14) centerX:0];
    //金额
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[self itemWithTitle:@"商品总额" des:model.original_price desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"运费" des:[NSString stringWithFormat:@"+¥%@", model.total_freight] desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"商品优惠" des:[NSString stringWithFormat:@"-¥%@", model.total_coupon_price] desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        if ([model.use_jifen isEqualToString:@"1"]) {
            [view view_addView:[self itemWithTitle:@"积分抵扣" des:[NSString stringWithFormat:@"-¥%@", model.jifen_money] desColor:nil] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        }
        [view view_addView:[self itemWithTitle:@"实付金额" des:[NSString stringWithFormat:@"¥%@", model.total_price] desColor:[RBThemeConfig priceRedColor]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(19) left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(14) right:Zoom(14) centerX:0];
    //订单状态
    NSString *statusString = @"";
    NSArray *actions;
    //订单状态 0未支付 1已支付待发货 2已发货待确认 3确认收货 4已评价 5退款 6已完成 7已取消
    switch ([model.order_status intValue]) {
        case 0 : {
            statusString = @"待付款";
            actions = @[
                @(RBGoodsOrderActionCancel),
                @(RBGoodsOrderActionPay)
            ];
        }   break;
        case 1 : {
            statusString = @"待发货";
            actions = @[];
        }   break;
        case 2 : {
            statusString = @"待收货";
            actions = @[
                @(RBGoodsOrderActionCheckLogistics),
                @(RBGoodsOrderActionConfirmReceive)
            ];
        }   break;
        case 3 : {
            statusString = @"待评价";
            actions = @[
                @(RBGoodsOrderActionCheckLogistics),
                @(RBGoodsOrderActionAppraise)
            ];
        }   break;
        case 4 : {
            statusString = @"已评价";
            actions = @[
                @(RBGoodsOrderActionCheckLogistics)
            ];
        }   break;
        case 5 : {
            statusString = @"退款";
            actions = @[];
        }   break;
        case 6 : {
            statusString = @"已完成";
            actions = @[
                @(RBGoodsOrderActionCheckLogistics)
            ];
        }   break;
        case 7 : {
            statusString = @"已取消";
            actions = @[];
        }   break;
    }
    self.title = statusString;
    
    [self addActions:actions];
}
//添加按钮
- (void)addActions:(NSArray *)actionsArray {
    [self.bottomView h_view_clear];
    if (actionsArray.count > 0) {
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(Zoom(40));
        }];
        [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.bottomView.mas_top);
        }];
        
        [self.bottomView h_view_addView:UILabel.new withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
        for (NSNumber *number in actionsArray) {
            [self.bottomView h_view_addView:[self buttonWithType:number.integerValue] withHeight:Zoom(30) width:Zoom(75) top:Zoom(5) bottom:Zoom(5) left:Zoom(15) right:Zoom(15) centerY:0];
        }
    }
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"order_id"] = self.order_id;
    } path:_Order_detail post:YES log:YES responseClass:RBGoodsOrderDetailResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBGoodsOrderDetailResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event
//支付
- (void)clickPayOrderAction {
    RBCashierVC *vc = RBCashierVC.new;
    vc.isFromOrder = YES;
    vc.order_id = self.order_id;
    vc.order_num = self.response.data.order_number;
    vc.order_price = self.response.data.total_price;
    LCWeakSelf;
    vc.block = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshListAction)]) {
            [weakSelf.delegate refreshListAction];
        }
        [weakSelf getData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
//取消订单
- (void)clickCancelOrderAction {
    LCWeakSelf;
    [JKAlert showTitle:@"提示" message:@"确定取消此订单?" cancelBtn:YES completion:^{
        [weakSelf viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_id"] = weakSelf.order_id;
        } path:_Order_cancel post:YES log:YES responseClass:RBResponse.class desView:weakSelf.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshListAction)]) {
                    [weakSelf.delegate refreshListAction];
                }
                [weakSelf.view showTip:@"取消成功" completion:^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }];
            }
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }];
}
//查看物流
- (void)clickCheckLogisticsAction {
    RBLogisticsVC *vc = RBLogisticsVC.new;
    vc.order_id = self.order_id;
    [self.navigationController pushViewController:vc animated:YES];
}
//去评价
- (void)clickAppraiseAction {
    RBOrderDetailModel *model = self.response.data;
    RBPublishEvaluateVC *vc = RBPublishEvaluateVC.new;
    vc.goodsModel = ({
        RBPublishEvaluateGoodsModel *goodsModel = RBPublishEvaluateGoodsModel.new;
        goodsModel.price = model.total_price;
        goodsModel.title = model.goods_name;
        goodsModel.picurl = model.file;
        goodsModel.type = 1;
        goodsModel.order_goods_id = model.order_goods_id;
        goodsModel;
    });
    LCWeakSelf;
    vc.block = ^{
        [weakSelf getData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
//确认收货
- (void)clickSureReceiveAction {
    LCWeakSelf;
    [JKAlert showTitle:@"提示" message:@"确定收到货物?" cancelBtn:YES completion:^{
        [weakSelf viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_id"] = weakSelf.order_id;
            dicM[@"status"] = @"3";
        } path:_Order_update post:YES log:YES responseClass:RBResponse.class desView:weakSelf.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshListAction)]) {
                    [weakSelf.delegate refreshListAction];
                }
                [weakSelf.view showTip:@"收货成功" completion:nil];
                [weakSelf getData];
            }
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }];
}
//再来一单
- (void)clickAgainOrderAction {
}

- (void)applySeriveBtnTapped
{
    __weak typeof(self) weakSelf = self;
    RBGoodsOrderApplyServicePopVC *vc = RBGoodsOrderApplyServicePopVC.new;
    vc.block = ^(NSInteger index) {
        if (index == 0)
        {
            RBGoodsOrderReundSubmitVC *vc = RBGoodsOrderReundSubmitVC.new;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        else
        {
            RBGoodsOrderServiceSubmitVC *vc = RBGoodsOrderServiceSubmitVC.new;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    };
    [self presentVc:vc];
}

#pragma mark - Setter

#pragma mark - Getter
//scrollView
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}
//商品信息
- (UIView *)shopViewWithData:(RBOrderDetailModel *)data {
    UIView *view = UIView.new;
    
    UIImageView *shopImgView = [RanTool imgViewWithMode:1 radius:0 imgName:@"groupPayShop"];
    [view addSubview:shopImgView];
    [shopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(Zoom(19));
        make.height.mas_equalTo(Zoom(17));
        make.left.mas_equalTo(Zoom(16));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *shopNameL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:data.store_name];
    [view addSubview:shopNameL];
    [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(160));
        make.left.mas_equalTo(shopImgView.mas_right).offset(Zoom(11));
    }];
    
    UIView *lineView = UIView.new;
    lineView.backgroundColor = [RBThemeConfig lineGrayColor];
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(16));
        make.right.mas_equalTo(-Zoom(16));
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    return view;
}
//商品信息
- (UIView *)goodsViewWithData:(RBOrderDetailModel *)data {
    UIView *view = UIView.new;
    
    UIImageView *imgView = [RanTool imgViewWithMode:2 radius:Zoom(5) imgName:data.file];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(15));
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(Zoom(97));
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.goods_name];
    nameL.numberOfLines = 2;
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(20));
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(15));
    }];
    
    UILabel *countL = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:2 text:[NSString stringWithFormat:@"x%@", data.quantity]];
    [countL setContentHuggingPriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [countL setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [view addSubview:countL];
    [countL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(nameL);
        make.top.mas_equalTo(nameL.mas_bottom).offset(Zoom(14));
        
    }];
    
    UILabel *priceL = UILabel.new;
    [view addSubview:priceL];
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameL);
        make.top.mas_equalTo(countL.mas_bottom).offset(Zoom(12));
    }];
    priceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
//        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textBlackColor] string:@"实付 "];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:data.total_price];
    }];

    return view;
}
//底部view
- (UIView *)bottomView {
    if (!_bottomView) {
        self.bottomView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.bottom.mas_equalTo(-kRBSafeBottomHeight);
                make.height.mas_equalTo(0);
            }];
            
            view;
        });
    }
    return _bottomView;
}
//按钮
- (UILabel *)buttonWithType: (RBGoodsOrderAction)type {
    UIColor *color;
    UIColor *bgColor;
    NSString *title;
    UIColor *borderColor;
    SEL sel;
    switch (type) {
        case RBGoodsOrderActionPay : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"去支付";
            sel = @selector(clickPayOrderAction);
        }   break;
        case RBGoodsOrderActionCheckDetail : {
        }   break;
        case RBGoodsOrderActionCancel : {
            color = [UIColor LC_hex:@"#666666"];
            bgColor = [UIColor LC_hex:@"#E2E2E2"];
            title = @"取消订单";
            sel = @selector(clickCancelOrderAction);
        }   break;
        case RBGoodsOrderActionCheckLogistics : {
            color = [UIColor LC_hex:@"#666666"];
            bgColor = [UIColor LC_hex:@"#E2E2E2"];
            title = @"查看物流";
            sel = @selector(clickCheckLogisticsAction);
        }   break;
        case RBGoodsOrderActionConfirmReceive : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"确认收货";
            sel = @selector(clickSureReceiveAction);
        }   break;
        case RBGoodsOrderActionAppraise : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"评价";
            sel = @selector(clickAppraiseAction);
        }   break;
        case RBGoodsOrderActionAgain : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"再来一单";
            sel = @selector(clickAgainOrderAction);
        }   break;
    }
    UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:color align:1 text:title bgColor:bgColor radius:Zoom(15)];
    [view addPriority:1];
    [view rbAddTapGesWithTarget:self action:sel];
    if (borderColor)
    {
        [view rbSetBorderWitdh:1 borderColor:borderColor];
    }
    return view;
}

- (UIView *)itemWithTitle: (NSString *)title des: (NSString *)des desColor: (UIColor *)desColor
{
    UIView *view = UIView.new;
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(13) weight:1 color:@"#454545" align:0 text:title];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(16));
        make.top.bottom.mas_equalTo(0);
    }];
    
    UILabel *desL = [RanTool labelWithFontSize:Zoom(13) weight:1 color:@"#333333" align:0 text:des];
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(Zoom(-16));
        make.top.bottom.mas_equalTo(0);
    }];
    if (desColor)
    {
        desL.textColor = desColor;
    }
    
    return view;
}

@end
